﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web; 
using System.Web.Mvc;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System;
using SRF.Common;
using System.Web.Mvc.Html;

namespace SRF.UI.Html
{
    /// <summary>
    /// 树形下来列表htmlHelper
    /// </summary>
    public static class TreeMenuExtensions
    {
        #region

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel">实体类型</typeparam>
        /// <typeparam name="TProperty">属性值的类型</typeparam>
        /// <typeparam name="TItem">列表实体类型</typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression">获取实体属性值的表达式</param>
        /// <param name="treeData">树形里边</param>
        /// <param name="getId"></param>
        /// <param name="getPId"></param>
        /// <param name="getName"></param>
        /// <param name="excludeId"></param>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TreeMenuFor<TModel, TProperty, TItem>(this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            IList<TItem> treeData, Func<TItem, string> getId, Func<TItem, string> getPId,
            Func<TItem, string> getName, string excludeId = "")
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            return MakeTreeMenuHtml<TItem>(metadata.PropertyName, metadata.Model, treeData,
                getId, getPId, getName, excludeId,
                htmlHelper.GetUnobtrusiveValidationAttributes(metadata.PropertyName, metadata));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel">实体类型</typeparam>
        /// <typeparam name="TProperty">属性值的类型</typeparam>
        /// <typeparam name="TItem">列表实体类型</typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression">获取实体属性值的表达式</param>
        /// <param name="treeData">树形里边</param>
        /// <param name="getId"></param>
        /// <param name="getPId"></param>
        /// <param name="getName"></param>
        /// <param name="excludeId"></param>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
        public static MvcHtmlString TreeMenu<TItem>(this HtmlHelper htmlHelper,
            string name, object value,
            IList<TItem> treeData, Func<TItem, string> getId, Func<TItem, string> getPId,
            Func<TItem, string> getName, string excludeId = "")
        {

            //var dd = new HtmlHelper<TreeNode>(htmlHelper.ViewContext, new ViewDataContainer<TModel>(model));
            return MakeTreeMenuHtml<TItem>(name, value, treeData, getId, getPId, getName, excludeId);
        }

        private static MvcHtmlString MakeTreeMenuHtml<TItem>(string name, object value, IList<TItem> treeData,
            Func<TItem, string> getId, Func<TItem, string> getPId,
            Func<TItem, string> getName, string excludeId,
            IDictionary<string, object> attributes = null)
        {
            StringBuilder htmlBuilder = new StringBuilder();

            foreach (TItem item in treeData)
            {

            }

          

            var html = "";// string.Format(htmlBuilder.ToString(), tagBuilder.ToString(TagRenderMode.SelfClosing), name, jsonDataBuilder, value);

            return new MvcHtmlString(html);
        }
         

        #endregion
    }

    public class TreeNode
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string ParentId { get; set; }
    }
}